const _html = function (body) {
  let html = ''
  if (Util.isIos() && Util.isWeixn()) {
    html = body
  } else {
    html = `<!DOCTYPE html>
   <html lang="en">

   <head>
       <meta charset="UTF-8">
       <style>
           * {
               margin: 0;
               padding: 0;
           }

           body {
               min-width: 375px;
               font-size:14px;
           }

           img {
               max-width: 100%;
           }
       </style>
   </head>

   <body>
   ${body}
   </body>

   </html>`
  }
  return html
}

export default class Util {
  static infoTab (goods) {
    let tab = []
    // 商品详情
    let goods_introduce = goods.goods_introduce
    if (goods.goods_introduce_mobile) {
      goods_introduce = JSON.parse(goods.goods_introduce_mobile)
    }
    if (goods_introduce) {
      tab.push({
        title: '商品详情',
        content: goods_introduce
      })
    }
    // 产品参数
    let goods_specifications = goods.goods_specifications
    if (goods.goods_specifications_mobile) {
      goods_specifications = JSON.parse(goods.goods_specifications_mobile)
    }
    if (goods_specifications) {
      tab.push({
        title: '产品参数',
        content: _html(Util.richtext(goods_specifications))
      })
    }
    // 售后保障
    let goods_after_sale = goods.goods_after_sale
    if (goods.goods_after_sale_mobile) {
      goods_after_sale = JSON.parse(goods.goods_after_sale_mobile)
    }
    if (goods_after_sale) {
      tab.push({
        title: '产品参数',
        content: _html(Util.richtext(goods_after_sale))
      })
    }
    return tab
  }

  static richtext (text) {
    let regex1 = new RegExp('(i?)(\<img)(?!(.*?style=[\'"](.*)[\'"])[^\>]+\>)', 'gmi')
    text = text ? text.replace(regex1, '$2 style=""$3') : text
    var regex2 = new RegExp('(i?)(\<img.*?style=[\'"])([^\>]+\>)', 'gmi')
    text = text ? text.replace(regex2, '$2display:block;max-width:100%;$3') : text
    return text
  }

  static isWeixn () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return ua.match(/wxwork/i) != 'wxwork'
    } else {
      return false
    }
  }

  static isUleeCommunityAPP () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/UleeCommunityAPP/i) == 'uleecommunityapp') {
      return true
    } else {
      return false
    }
  }

  static isIos () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/iPhone/i) == 'iphone') {
      return true
    } else {
      return false
    }
  }

  static isAndroid () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/Android/i) == 'android') {
      return true
    } else {
      return false
    }
  }

  static setupWebViewJavascriptBridge (callback) {
    if (Util.isIos()) {
      if (window.WebViewJavascriptBridge) {
        // eslint-disable-next-line no-undef
        return callback(WebViewJavascriptBridge)
      }
      if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback)
      }
      window.WVJBCallbacks = [callback] // 创建一个 WVJBCallbacks 全局属性数组，并将 callback 插入到数组中。
      var WVJBIframe = document.createElement('iframe') // 创建一个 iframe 元素
      WVJBIframe.style.display = 'none' // 不显示
      WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__' // 设置 iframe 的 src 属性
      document.documentElement.appendChild(WVJBIframe) // 把 iframe 添加到当前文导航上。
      setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
      }, 0)
    }
    if (Util.isAndroid()) {
      if (window.WebViewJavascriptBridge) {
        // callback()
        // eslint-disable-next-line no-undef
        callback(WebViewJavascriptBridge)
      } else {
        // document.addEventListener(
        //   'WebViewJavascriptBridgeReady',
        //   // callback,
        //   function () {
        //     // eslint-disable-next-line no-undef
        //     callback(WebViewJavascriptBridge)
        //   },
        //   false
        // )
        document.addEventListener('WebViewJavascriptBridgeReady', function (event) {
          // eslint-disable-next-line no-undef
          if (window.onWebViewJavascriptBridgeReady) window.onWebViewJavascriptBridgeReady(window.__bridge = WebViewJavascriptBridge)
          // eslint-disable-next-line no-undef
          callback(WebViewJavascriptBridge)
        }, false)
      }
    }
  }

  /**
   * @desc 获取导航栏’？‘后面的参数
   */
  static UrlParser () {
    let query = {}
    let url = []
    if (window.location.search.slice(1)) {
      url = location.search.slice(1).split('&')
    } else if (window.location.hash.slice(1).split('?')[1]) {
      url = location.hash.slice(1).split('?')[1].split('&')
    } else {
      return undefined
    }
    url.forEach(function (item) {
      let items = item.split('=')
      query[items[0]] = items[1]
    })
    return query
  }
}
